import java.util.Arrays;

/*
    3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of
    different sizes which can slide onto any tower. The puzzle starts with disks sorted
    in ascending order of size from top to bottom (i.e., each disk sits on top of an
    even larger one). You have the following constraints:
    (1) Only one disk can be moved at a time.
    (2) A disk is slid off the top of one tower onto the next tower.
    (3) A disk can only be placed on top of a larger disk.
    Write a program to move the disks from the first tower to the last using stacks.
*/
public class HanoiTower<T extends Comparable<T>> {
    private final HanoiTowerStack<T>[] towers = new HanoiTowerStack[3];

    public HanoiTower(T... values) {
        towers[0] = new HanoiTowerStack<T>(0, values);
        for (int i = 1; i < 3; i++) {
            towers[i] = new HanoiTowerStack<T>(i, values.length);
        }
    }

    @Override
    public String toString() {
        return "HanoiTower{" +
                "towers=" + Arrays.toString(towers) +
                '}';
    }

    public void moveTopTo(int sourceTowerId, int targetTowerId) {
        if (sourceTowerId < 0 || sourceTowerId >= towers.length)
            throw new ArrayIndexOutOfBoundsException("Index out of boud " + sourceTowerId);
        if (targetTowerId < 0 || targetTowerId >= towers.length)
            throw new ArrayIndexOutOfBoundsException("Index out of boud " + targetTowerId);

        if (sourceTowerId == targetTowerId)
            throw new IllegalArgumentException("The indexes are the same: " + sourceTowerId);
        final HanoiTowerStack<T> sourceTower = towers[sourceTowerId];
        final HanoiTowerStack<T> targetTower = towers[targetTowerId];
        final T element = sourceTower.stack.pop();
        targetTower.stack.push(element);
    }

    public void solveTheProblem() {
        moveSomeDisks(towers[0].stack.size, 0, 1, 2);
    }

    private void moveSomeDisks(int amount, int sourceId, int bufferId, int destinId) {
        if (amount <= 0) return;
        moveSomeDisks(amount - 1, sourceId, destinId, bufferId);
        moveTopTo(sourceId, destinId);
        moveSomeDisks(amount - 1, bufferId, sourceId, destinId);
    }

    private static class HanoiTowerStack<T> {
        private final int id;
        private StackImpl<T> stack;

        public HanoiTowerStack() {
            this.id = 0;
            this.stack = new StackImpl<T>();
        }

        public HanoiTowerStack(int id, T... values) {
            this.id = id;

            this.stack = new StackImpl<T>(values);
        }

        public HanoiTowerStack(int id, int size) {
            this.id = id;
            stack = new StackImpl<T>(size);
        }

        public static void main(String[] args) {

        }

        @Override
        public String toString() {
            return "HanoiTowerStack{" +
                    "id=" + id +
                    ", stack=" + stack +
                    '}';
        }
    }
}
